import { ServerError } from "..";

/** 校验是否请求成功 */
export async function isRequestSuccess(data: Response, options?: IIsRequestSuccess) {
    if (options?.isCheckRequestSuccess === false) return data;
    
    try {
        if (!data.ok) {
            const r = await data.json();
            throw new ServerError(r?.message || '请求错误', r);
        }
    } catch (error) {
        throw error instanceof ServerError ? error : new ServerError('请求失败', error);
    }

    return data;
}

export interface IIsRequestSuccess {

    /** 是否校验请求成功 */
    isCheckRequestSuccess?: boolean
}